package com.ruoyi.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.charge.domain.Record;
import com.ruoyi.charge.domain.WxUser;
import com.ruoyi.charge.mapper.RecordMapper;
import com.ruoyi.charge.mapper.WxUserMapper;
import com.ruoyi.charge.service.IWxUserService;
import com.ruoyi.charge.vo.WxUserVo;
import com.ruoyi.charge.bo.WxUserAddBo;
import com.ruoyi.charge.bo.WxUserEditBo;
import com.ruoyi.charge.bo.WxUserQueryBo;
import com.ruoyi.common.enums.DeletedStatus;
import com.ruoyi.common.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 微信用户管理Service业务层处理
 *
 * @author fanqie
 * @date 2021-11-01
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements IWxUserService {

    @Autowired
    private WxUserMapper wxUserMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public WxUserVo queryById(Long wxUserId) {
        WxUser db = this.baseMapper.selectById(wxUserId);
        return BeanUtil.toBean(db, WxUserVo.class);
    }

    @Override
    public List<WxUserVo> queryList(WxUserQueryBo bo) {
        LambdaQueryWrapper<WxUser> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getName()), WxUser::getName, bo.getName());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), WxUser::getPhone, bo.getPhone());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<WxUserVo> entity2Vo(Collection<WxUser> collection) {
        List<WxUserVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, WxUserVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<WxUser> page = (Page<WxUser>) collection;
            Page<WxUserVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(WxUserAddBo bo) {
        String openid = bo.getOpenid();
        WxUser wxUser = wxUserMapper.selectOne(
                new LambdaQueryWrapper<WxUser>().eq(WxUser::getOpenid, openid)
                .eq(WxUser::getDeleted, DeletedStatus.VALID.getCode())
                .last("limit 1")
        );
        if(wxUser == null){
            WxUser add = BeanUtil.toBean(bo, WxUser.class);
            validEntityBeforeSave(add);
            this.save(add);
        }
        return true;
    }

    @Override
    public Boolean updateByEditBo(WxUserEditBo bo) {
        WxUser update = BeanUtil.toBean(bo, WxUser.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(WxUser entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public String getChargeStrCode(Long recordId) {
        Record record = recordMapper.selectById(recordId);
        if(record==null){
            throw new CustomException("记录id不存在！");
        }

        return record.getRemark();
    }
}
